<script>
/*
 * @Author: Your name
 * @Date:   2020-07-26 15:11:07
 * @Last Modified by:   Your name
 * @Last Modified time: 2020-07-26 15:35:32
 */
</script>
<template>
  <div class="bottom-menu">
    <check-button class="select-all" @click.native="selectAll" :is-checked="isSelectedAll"></check-button>
    <span>全选</span>
    <span class="total-price">合计: {{totlePrice}}</span>
    <span class="buy-product">去计算({{totleGoods}})</span>
  </div>
</template>
<script>
import CheckButton from './CheckButton.vue';
export default {
  name: 'BottomBar',
  components: {
    CheckButton
  },
  props: {
    cartList: {
      type: Array,
      default(){
        return []
      }
    }
  },
  computed: {
    totlePrice(){
      return '￥' + this.cartList.filter(item => {
        return item.isSelect;
      }).reduce((preValue, item) => {
        return preValue + item.newPrice * item.count;
      }, 0).toFixed(2);
    },
    totleGoods() {
      return this.cartList.filter(item => item.isSelect).length;
    },
    //判断全选按钮是否全选
    isSelectedAll(){
      //如果列表中未选中按钮构成的数组长度为0，那么0取反变成true表示全选按钮别选中。
      return !this.cartList.filter(item => !item.isSelect).length;
    }
  },
  methods: {
    selectAll(){
      if(this.isSelectedAll){
        this.cartList.forEach(item => item.isSelect = false)
      }else{
        this.cartList.forEach(item => item.isSelect = true)
      }
    }
  }
}
</script>
<style lang="css">
  .bottom-menu {
    width: 100%;
    height: 44px;
    background-color: #eee;
    position: fixed;
    bottom: 50px;
    left: 0;
    box-shadow: 0 -2px 3px rgba(0, 0, 0, .2);
    font-size: 14px;
    color: #888;
    line-height: 44px;
    padding-left: 35px;
    box-sizing: border-box;
  }

  .bottom-menu .select-all {
    position: absolute;
    line-height: 0;
    left: 12px;
    top: 13px;
  }

  .bottom-menu .total-price {
    margin-left: 15px;
    font-size: 16px;
    color: #666;
  }

  .bottom-menu .buy-product {
    background-color: orangered;
    color: #fff;
    width: 100px;
    height: 44px;
    text-align: center;
    line-height: 44px;
    float: right;
  }
</style>